<?php

namespace app\admin\controller;


use app\admin\model\Purchase;
use cmf\controller\AdminBaseController;
use OpenApi\Annotations\Examples;
use think\facade\Db;

/**
 * Class PurchaseController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   =>'采购合同管理',
 *     'action' =>'index',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>''
 * )
 */
class PurchaseController extends AdminBaseController
{

    public function index()
    {
        //搜索部分
//        $key_status = input('get.key_status/d');

        $lists = Purchase::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);
        $user_arr = Db::name('user')
            ->column('user_nickname', 'id');

        $this->assign('user_arr', $user_arr);
        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(), 'domain' => request()->domain() . '/upload/']);
    }

    public function add()
    {
        $steel_grade_id_arr = Db::query('select id,name from cmf_steel_grade where delete_time =0');
        $this->assign('steel_grade_arr_da1', json_encode($steel_grade_id_arr));
        return $this->fetch('', ['domain' => request()->domain() . '/upload/']);
    }

    public function addPost()
    {

        $params = input('post.');

        if (!$params) {
            $this->error('非法参数');
        }
        $params['order_time'] = strtotime($params['order_time']);
        $params['update_time'] = time();
        $params['add_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
        if (isset($params['date'])) {

            $data = $params['date'];
//var_dump($data);
//exit();
//            Db::name('purchase_sub')->insertGetId($params['date'][100]);
//            $id = Db::name('purchase_sub')->select();
//            var_dump($id);
//            var_dump(Db::name('purchase_sub')->getLastSql());
//            exit();
            unset($params['date']);

//            Db::startTrans();

            $id = Db::name('purchase')->insertGetId($params);

            if ($id) {

                foreach ($data as $k => $v) {
                    $newdata = array();
//                    var_dump(json_encode($v));
                    $newdata = $v;
//                    var_dump($newdata);
//                    $newdata['order_id'] = $v['order_id'];
//                    $newdata['c_name'] = $v['c_name'];
//                    var_dump($newdata);
//                    exit();
//                    $newdata['order_id'] = $v['order_id'];
//
//                    $newdata['c_name'] = $v['c_name'];
//                    $newdata['steel_grade_id'] = $v['steel_grade_id'];
//                    $newdata['coil_num'] = $v['coil_num'];
//                    $newdata['d'] = $v['d'];
//                    $newdata['w'] = $v['w'];
//                    $newdata['h'] = $v['h'];
//                    $newdata['num'] = $v['num'];
//                    $newdata['weight'] = $v['weight'];
//                    $newdata['arr_num'] = $v['arr_num'];
//
//                    $newdata['arr_weight'] = $v['arr_weight'];
////                    $newdata['differ'] = $v['differ'];
////                    var_dump($newdata);
////                    exit();
//                    $newdata['unit_price'] = $v['unit_price'];
//
//                    $newdata['steelmill'] = $v['steelmill'];
//                    $newdata['delivery_month'] = $v['delivery_month'];
//                    $newdata['arr_place'] = $v['arr_place'];


                    $newdata['purchase_id'] = $id;
                    $newdata['update_time'] = time();
                    $newdata['add_time'] = time();
                    $newdata['admin_id'] = cmf_get_current_admin_id();
                    if ($v['weight'] == '') {
                        $v['weight'] = 0;
                    }
                    if ($v['arr_weight'] == '') {
                        $v['arr_weight'] = 0;
                    }
//                    var_dump(  $v['weight'] );
//                    var_dump(  $v['arr_weight'] );
//var_dump();
//
                    $newdata['differ'] = $v['weight'] - $v['arr_weight'];
//                    var_dump($newdata);
//                    exit();
                    Db::name('purchase_sub')->insertGetId($newdata);
                }


//                Db::commit();
                $this->success('操作成功', url('index'));
            } else {
//                Db::rollback();
                $this->error('操作失败');
            }

        } else {
            $this->error('请添加子项');
        }

    }

    public function edit()
    {
        $id = input('get.id/d');

        if (!is_numeric($id)) {
            $this->error('非法参数');
        }
        $steel_grade_id_arr = Db::query('select id,name from cmf_steel_grade where delete_time =0');
        $this->assign('steel_grade_arr_da1', json_encode($steel_grade_id_arr));
        $res = Purchase::where('id', $id)->find();
        $pasub = Db::name('purchase_sub')->where('purchase_id', $id)->select();
        $this->assign('pasub', $pasub);
        return $this->fetch('', ['vo' => $res, 'domain' => request()->domain() . '/upload/']);
    }

    public function editPost()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params || empty($params['id'])) {
            $this->error('非法参数');
        }
        $params['update_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
        Purchase::update($params);

        $this->success('操作成功');
    }

    public function del()
    {
        $id = input('get.id/d');

        if (!(is_numeric($id) && $id > 0)) {
            $this->error('非法参数');
        }

        Purchase::where('id', $id)->update(array('delete_time' => time(), 'admin_id' => cmf_get_current_admin_id()));

        $this->success('删除成功');
    }

    public function field()
    {
        $id = input('get.id/d');
        $field = input('get.field');

        if (!(is_numeric($id) && $id > 0 && $field)) {
            $this->error('非法参数');
        }

        $info = Purchase::field($field)->where('id', $id)->find();

        $this->success('ok', '', $info[$field]);
    }
}